﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace Expression.Samples.PathListBoxUtils
{
	using System.Windows;
	using Microsoft.Expression.Controls;

	public static class PathListBoxExtensions
	{
		/// <summary> 
		/// Determines the number of items currently arranged on the specified layout path.</summary>
		/// <param name=”layoutPathIndex”> The specified layout path.</param>
		/// <returns> The number of items arranged.</returns>
		public static int GetItemsArrangedCount(this PathListBox pathListBox, int layoutPathIndex)
		{
			if (pathListBox == null)
			{
				return 0;
			}
			int count = 0;
			PathListBoxItem pathListBoxItem;
			for (int i = 0; i < pathListBox.Items.Count; i++)
			{
				pathListBoxItem = (PathListBoxItem)pathListBox.ItemContainerGenerator.ContainerFromItem(pathListBox.Items[i]);
				if (pathListBoxItem != null && (layoutPathIndex == -1 || pathListBoxItem.LayoutPathIndex == layoutPathIndex))
				{
					count++;
				}
			}
			return count;
		}

		/// <summary> 
		/// Finds the item that is laid out at the beginning of the specified layout path.</summary>
		/// <param name=”layoutPathIndex”> The specified layout path.</param>
		/// <returns> The global index of the item.</returns>
		public static int GetFirstArrangedIndex(this PathListBox pathListBox, int layoutPathIndex)
		{
			if (pathListBox == null)
			{
				return 0;
			}
			PathListBoxItem pathListBoxItem;
			PathListBoxItem firstItemArranged = null;
			int firstArrangedIndex = 0;
			for (int i = 0; i < pathListBox.Items.Count; i++)
			{
				pathListBoxItem = (PathListBoxItem)pathListBox.ItemContainerGenerator.ContainerFromIndex(i);
				if (pathListBoxItem != null && pathListBoxItem.LayoutPathIndex == layoutPathIndex)
				{
					if (firstItemArranged == null)
					{
						if (pathListBoxItem.IsArranged)
						{
							firstItemArranged = pathListBoxItem;
							firstArrangedIndex = firstItemArranged.GlobalIndex;
						}
					}
					else
					{
						if (pathListBoxItem.LocalIndex < firstItemArranged.LocalIndex &&
						pathListBoxItem.Visibility == Visibility.Visible && pathListBoxItem.IsArranged)
						{
							firstItemArranged = pathListBoxItem;
							firstArrangedIndex = firstItemArranged.GlobalIndex;
						}
					}
				}
			}
			return firstArrangedIndex;
		}

		/// <summary> 
		/// Finds the item that is laid out at the end of the specified layout path.</summary>
		/// <param name=”layoutPathIndex”> The specified layout path.</param>
		/// <returns> The global index of the item.</returns>
		public static int GetLastArrangedIndex(this PathListBox pathListBox, int layoutPathIndex)
		{
			if (pathListBox == null)
			{
				return 0;
			}
			
			PathListBoxItem pathListBoxItem;
			PathListBoxItem lastItemArranged = null;
			int lastArrangedIndex = 0;
			for (int i = 0; i < pathListBox.Items.Count; i++)
			{
				pathListBoxItem = (PathListBoxItem)pathListBox.ItemContainerGenerator.ContainerFromIndex(i);
				if (pathListBoxItem != null && pathListBoxItem.LayoutPathIndex == layoutPathIndex)
				{
					if (lastItemArranged == null)
					{
						if (pathListBoxItem.IsArranged)
						{
							lastItemArranged = pathListBoxItem;
							lastArrangedIndex = lastItemArranged.GlobalIndex;
						}
					}
					else
					{
						if (pathListBoxItem.LocalIndex > lastItemArranged.LocalIndex &&
						pathListBoxItem.Visibility == Visibility.Visible && pathListBoxItem.IsArranged)
						{
							lastItemArranged = pathListBoxItem;
							lastArrangedIndex = lastItemArranged.GlobalIndex;
						}
					}
				}
			}
			return lastArrangedIndex;
		}
	}
}
